<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class AddItemPage {
	private $alerter;
	private $item_functions;
	private $access;
	private $toolbox;
	
	public function __construct() {
		$this->alerter = new Alerter();
		$this->item_functions = new ItemFunctions();
		$this->access = new AccessManager();
		$this->toolbox = new Toolbox();
	}

	public function set_access_manager($access) {
		$this->access = $access;
	}

	public function set_alerter($alerter) {
		$this->alerter = $alerter;
	}

	public function set_toolbox($toolbox) {
		$this->toolbox = $toolbox;
	}

	public function set_item_functions($item_functions) {
		$this->item_functions = $item_functions;
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		$this->access->check_access("READ");
	}

	/**
	 * appropriate page action interpretations
	 */
	function page_inc_execute_action($action) {
		switch($action) {
			default:
				$this->alerter->add_alert("unknown action");
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {

		global $filename;
		$THISDIR = $page->directory_tree[$page->dir_depth - 1];

		if (!$THISDIR['show_items']) {
			$this->toolbox->endpage($page, "Hier kunnen geen items toegevoegd worden");
		}

		if ($_POST['submitted']) {
			
			// check submitted content
			$itemType = new ItemType();
			if ($this->item_functions->item_page_inc_check_content(
					$page, $_POST, $_FILES, $itemType->get_type_string($_POST['item_type_id']), 1)) {
			
				$baseTopic = BaseTopic::new_topic($page, true, $_POST['item_type_id']);
				$baseTopic->create_new();

				$item_id = $baseTopic->get_id();
				$new_url = $page->dir_path . "/$item_id." . $itemType->get_type_string($_POST['item_type_id']);

				// process subscriptions
				$message = array(
					'alert' => "{$_SESSION['member']['nick']} plaatste een item in '<a href={$new_url}>{$THISDIR['title']}</a>' <span class=iteminfo>(je krijgt dit bericht omdat je je abonneerde op de directory '{$THISDIR['title']}'. Vanuit de directory zelf kan je je abonnement opzeggen)</span>",
					'subject' => "Nieuw item in '{$THISDIR['title']}'",
					'mail' => "{$_SESSION['member']['nick']} plaatste een nieuw item in '{$THISDIR['title']}': '<a href={$new_url}>{$_POST['title']}</a>'\n<quote author={$_SESSION['member']['nick']}>{$_POST['body']}</quote>\n<p>(je krijgt dit bericht omdat je je abonneerde op de directory '{$THISDIR['title']}'. Vanuit de directory zelf kan je je abonnement opzeggen)",
					'email' => ereg_replace("<quote.+</quote>", "", "{$_SESSION['member']['nick']} plaatste een nieuw item in '{$THISDIR['title']}': '{$_POST['title']}'\ndirectory: {$CONFIG['base_url']}{$page->dir_path}\nitem: {$CONFIG['base_url']}{$new_url}\n\n---------------------\n{$_POST['body']}\n---------------------\n\n(je krijgt dit bericht omdat je je abonneerde op de directory '{$THISDIR['title']}'. Vanuit de directory zelf kan je je abonnement opzeggen)"));
				$this->item_functions->handle_subscriptions("DIR", $THISDIR['dir_id'], $message);

				$this->toolbox->redirect($new_url);
			}
		}
			
		$page->content['title'] = "Een item toevoegen";

		$this->item_functions->item_form($page);
	}


	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		print_form($page, $page->content['form']);
	}
}

?>
